﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.Text;

namespace Qplus.protocol
{
    public abstract class OutPacket : TCPPackage
    {
        ByteBuffer bodyBuf;
        byte[] data = null;

        public void init()
        {
            bodyBuf = new ByteBuffer();
            putHeadFirst((byte)0);
            putHeadLength(0);
            putHeadID(msgID);
        }

        /**
	    * 当body写完后填写包长度，最后调用，并且必须调用
	    * @param len
	    */
        protected void postLen(int len)
        {
            data = bodyBuf.ToByteArray();
            byte[] l = DataUtil.twoToBytes(len);
            data[1] = l[0];
            data[2] = l[1];
        }



        /**
	    * 将8位数写入buffer
	    * @param num1
	    */
        protected int put1(byte num1)
        {
            bodyBuf.PushByte(num1);
            return 1;
        }


        /**
	     * 将16位数写入buffer
	     * @param num1
	     */
        protected int put2(int num1)
        {
            bodyBuf.PushByteArray(DataUtil.twoToBytes(num1));
            return 2;
        }


        /**
	 ```* 将32位数写入buffer
	 ```* @param num4
	 ```*/
        protected int put4(long num4)
        {
            bodyBuf.PushByteArray(DataUtil.fourToBytes(num4));
            return 4;
        }


        /**
	    * 将数组写入buffer
	    * @param data
	    * @param offset
	    * @param len
	    */
        protected int putN(byte[] data, int offset, int len)
        {
            bodyBuf.PushByteArray(data);
        
            //bodyBuf.append(data, offset, len);
            return len;
        }


        /**
	     * 将长度小于8位的字符串写入buffer
	     * @param num1
	     */
        protected int putString1(String str)
        {
            int len = 0;
            byte[] strs = Encoding.UTF8.GetBytes(str);
            len += put1((byte)strs.Length);
            len += putN(strs, 0, strs.Length);
            return len;
        }

        /**
        * 将长度小于8位的字符串写入buffer
        * @param num1
        */
        protected int putVersion(String str)
        {
            int len = 0;
            byte[] strs = Encoding.UTF8.GetBytes(str);
            len += putN(strs, 0, strs.Length);
            return len;
        }

        /**
	     * 将长度小于8位的字符串写入buffer
	     * @param num1
	     */
        protected int putString2(String str)
        {
            int len = 0;
            byte[] strs = Encoding.UTF8.GetBytes(str);
            len += putN(strs, 0, strs.Length);
            return len;
        }




        /**
         * 写入头
         * @param first
         */
        protected void putHeadFirst(byte first)
        {
            put1(first);
        }

        /**
       * 写入包长度
       * @param len
       */
        protected void putHeadLength(int len)
        {
            put2(len);
        }

        /**
       * 写入消息id
       * @param id
       */
        protected void putHeadID(int id)
        {
            put2(id);
        }

        /**
	    * 获取打包好后的数据
	    * @return
	    */
        public byte[] getData()
        {
            return data;
        }

	   // protected override void handle(byte[] data, int offset, int count) {
       //     return;
	   // }


	    public TCPPackage clone() {
            //using (Stream objectStream = new MemoryStream())
            //{
            //    IFormatter formatter = new BinaryFormatter();
            //    formatter.Serialize(objectStream, this);
            //    objectStream.Seek(0, SeekOrigin.Begin);
            //    return formatter.Deserialize(objectStream) as TCPPackage;
            //}
            return Clone() as TCPPackage;
            //return TCPPackage.MemberwiseClone();
	}

    }
}
